//
//  main.m
//  05-循环遍历
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
    NSArray *stringArr = @[@"小",@"b",@"c"];
    NSMutableArray *marr = [NSMutableArray array];
    for (NSInteger i = stringArr.count - 1; i >= 0; i--) {
        [marr addObject:stringArr[i]];
        
    }
    
    NSDictionary *person = @{@"name":@"xiao",
                             @"age":@"10",
                             @"sex":@"male"};
    NSArray *arr = [person allKeys];
    for (int i = 0; i < arr.count; i++) {
        NSString *key = arr[i];
        NSString *value = [person objectForKey:key];
        NSLog(@"23112123:%@",value);
    }
    for (NSString *str in arr) {
        NSString *value = [person objectForKey:str];
        NSLog(@"%@",value);
    }
    //枚举器
    // NSEnumerator 依赖于集合类型 （数组  字典 集合），不能单独使用
    NSEnumerator *enume = [arr objectEnumerator];
    id value = nil;
    while ((value = [enume nextObject]) != nil) {
        NSLog(@"value= %@",value);
    }
    //倒序
    NSEnumerator *reverse = [marr reverseObjectEnumerator];
    id value1 = nil;
    while (value1 = [reverse nextObject]) {
        NSLog(@"value1:%@",reverse);
    }
    
    
    
    
    
    //快速枚举 for...in
    for (NSString *str  in arr) {
        NSLog(@"%@",str);
    }
    
    NSArray *Arr = @[@"a",@"b",@"c",@"d",@"e"];
    NSString *appendstr = [NSString string];
    for (NSString *str  in Arr) {
        appendstr = [appendstr stringByAppendingString:str];
    }
    NSLog(@"dfdfdsa:%@",appendstr);
    // 排序
    // 参数1 key:排序条件
    // 参数2 acending: YES or NO (降序)
    // 数组邮排序的方法 （sortedArrayUsingDescriptors）
    //参数为一个排序的数组
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"self" ascending:YES];
            Arr = [Arr sortedArrayUsingDescriptors:@[sortDescriptor]];
    NSLog(@"%@",Arr);
    NSMutableArray *marr1 = [NSMutableArray arrayWithObjects:@11,@12,@3,@33,@45,nil];
     [marr1 sortUsingDescriptors:@[sortDescriptor]];
    NSLog(@"marr1%@",marr1);
    
    
    //排序：可变数组和不可变数组通过sort排序调用方法不同
    // 1.不可变数组, 排序后需要生成新的数组
    // 2.可变数组, 排序直接可以在本身之上修改
    Person *per = [Person personWithName:@"zhangsan" age:10];
    Person *per1 = [Person personWithName:@"zisan" age:15];
    Person *per2 = [Person personWithName:@"changsan" age:13];
    
    NSArray *arrOfPer = @[per,per1,per2];
    NSSortDescriptor *sort = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
    arrOfPer = [arrOfPer sortedArrayUsingDescriptors:@[sort]];
    NSLog(@"%@",arrOfPer);
    
    //排序2:
    NSArray *sortSting = @[@"iPhone",@"iPad",@"iTouch",@"iMac",@"Mac Mini"];
    sortSting = [sortSting sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@",sortSting);
    
    
    
    
    
    
    return 0;
}
